package wang.hex.codetransfer.code;

/**
 * @Author wujie
 * @Class BytesUtil
 * @Description
 * @Date 2021/1/6 10:12
 */
public class BytesUtil {
    public static void setLong2Bytes(long value, byte[] bytes, int offset) {
        bytes[offset + 0] = (byte) ((value >> 56) & 0xFF);
        bytes[offset + 1] = (byte) ((value >> 48) & 0xFF);
        bytes[offset + 2] = (byte) ((value >> 40) & 0xFF);
        bytes[offset + 3] = (byte) ((value >> 32) & 0xFF);
        bytes[offset + 4] = (byte) ((value >> 24) & 0xFF);
        bytes[offset + 5] = (byte) ((value >> 16) & 0xFF);
        bytes[offset + 6] = (byte) ((value >> 8) & 0xFF);
        bytes[offset + 7] = (byte) (value & 0xFF);
    }

    public static int getIntFromBytes(byte[] bytes, int offset) {
        int res = bytes[offset + 3] & 0xFF |
                (bytes[offset + 2] & 0xFF) << 8 |
                (bytes[offset + 1] & 0xFF) << 16 |
                (bytes[offset + 0] & 0xFF) << 24;
        return res;
    }

    public static long getLongFromBytes(byte[] bytes, int offset) {
        long res = bytes[offset + 7] & 0xFF |
                (bytes[offset + 6] & 0xFF) << 8 |
                (bytes[offset + 5] & 0xFF) << 16 |
                (bytes[offset + 4] & 0xFF) << 24 |
                (bytes[offset + 3] & 0xFF) << 32 |
                (bytes[offset + 2] & 0xFF) << 40 |
                (bytes[offset + 1] & 0xFF) << 48 |
                (bytes[offset + 0] & 0xFF) << 56;
        return res;
    }

    public static void setInt2Bytes(int a, byte[] bytes, int offset) {
        bytes[offset + 0] = (byte) ((a >> 24) & 0xFF);
        bytes[offset + 1] = (byte) ((a >> 16) & 0xFF);
        bytes[offset + 2] = (byte) ((a >> 8) & 0xFF);
        bytes[offset + 3] = (byte) (a & 0xFF);
    }


}
